package com.supercube.wechat.controller;

import com.supercube.common.controller.BaseController;
import com.supercube.common.controller.IPageDefine;
import com.supercube.common.utils.PageUtils;
import com.supercube.common.utils.Query;
import com.supercube.common.utils.R;
import com.supercube.wechat.domain.NotificationDO;
import com.supercube.wechat.service.NotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 小程序通知消息表
 * @author djzhao
 * @date 2019-10-30 20:25:58
 */
 
@Controller
@RequestMapping("/wechat/notification")
public class NotificationController extends BaseController {
	@Autowired
	private NotificationService notificationService;
	
	@GetMapping()
	//@RequiresPermissions("wechat:notification:notification")
	String Notification(){
	    return "wechat/notification/notification_list";
	}

	/**
	 * 更新需要展示的数量
	 * @param showNum
	 * @return
	 */
	@PostMapping("/updateShowNum")
	public String updateShowNum(int showNum) {
		notificationService.updateShowNum(showNum);
		return "redirect:wechat/notification";
	}


	/**
	 * 分页列表查询
	 */
	@ResponseBody
	@GetMapping("/listPage")
	//@RequiresPermissions("wechat:notification:notification")
	public Map list(@RequestParam Map<String, Object> params){

		Map<String, Object> map = new HashMap<>();

		//根据分页参数(格式：{limit=10, offset=0} )，然后进行分页查询
		PageUtils pageUtils = getPageList(params, new IPageDefine() {

			@Override
			public List<?> getPageRows(Query query) {
				return notificationService.list(query);
			}
		});
		map.put("total", pageUtils.getTotal());
		map.put("rows", pageUtils.getRows());
		map.put("showNum", notificationService.getShowNum());
		return map;
	}
	
	/**
	 * 跳转到新增页面
	 */
	@GetMapping("/add")
	//@RequiresPermissions("wechat:notification:add")
    ModelAndView add(){
	    ModelAndView mv = new ModelAndView();
		mv.addObject("notification", new NotificationDO());
		mv.setViewName("wechat/notification/notification_edit");
		return mv;
	}
	
	/**
	 * 跳转到修改页面
	 */
	@GetMapping("/edit/{id}")
	//@RequiresPermissions("wechat:notification:edit")
    ModelAndView edit(@PathVariable("id") Integer id){
	    ModelAndView mv = new ModelAndView();
		NotificationDO notification = notificationService.get(id);
		mv.addObject("notification", notification);
		mv.setViewName("wechat/notification/notification_edit");
		return mv;
	}
	
	/**
	 * 新增方法
	 */
	@ResponseBody
	@PostMapping("/save")
	//@RequiresPermissions("wechat:notification:add")
	public R save( NotificationDO notification){
		if(notificationService.save(notification)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改方法
	 */
	@ResponseBody
	@RequestMapping("/update")
	//@RequiresPermissions("wechat:notification:edit")
	public R update( NotificationDO notification){
		notificationService.update(notification);
		return R.ok();
	}
	
	/**
	 * 单条删除方法
	 */
	@PostMapping( "/remove")
	@ResponseBody
	//@RequiresPermissions("wechat:notification:remove")
	public R remove( Integer id){
		if(notificationService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 批量删除方法
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	//@RequiresPermissions("wechat:notification:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		notificationService.batchRemove(ids);
		return R.ok();
	}
	
}
